package com.umbrella.worker.web.controller.manager;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.umbrella.worker.dto.AccountRecordDO;
import com.umbrella.worker.dto.OrderDO;
import com.umbrella.worker.dto.OrderDetailDO;
import com.umbrella.worker.dto.StaffDO;
import com.umbrella.worker.query.OrderQuery;
import com.umbrella.worker.query.StaffQuery;
import com.umbrella.worker.query.SupplierQuery;
import com.umbrella.worker.result.JsonResultDO;
import com.umbrella.worker.result.JsonResultSupport;
import com.umbrella.worker.result.ResultDO;
import com.umbrella.worker.result.ResultSupport;
import com.umbrella.worker.service.IOrderService;
import com.umbrella.worker.service.IStaffService;
import com.umbrella.worker.service.ISupplierAccountService;
import com.umbrella.worker.service.ISuppliersService;
import com.umbrella.worker.util.PageBeanUtil;

@Controller
@RequestMapping(value = "/morder")
public class ManagerOrderController {

	@Autowired
	private IOrderService orderService;

	@Autowired
	private ISuppliersService suppliersService;

	@Autowired
	private IStaffService staffService;
	
	@Autowired
	private ISupplierAccountService supplierAccountService;

	@RequestMapping(value = "/waitOrderList.html", method = RequestMethod.GET)
	public ModelAndView waitOrderList(ModelAndView mav, HttpServletRequest request) {
		mav.setViewName("manager2/order/waitOrderList");
		return mav;
	}

	@RequestMapping(value = "/waitOrderList.json", method = RequestMethod.GET)
	public ModelAndView ajaxWaitOrderList(ModelAndView mav, HttpServletRequest request) {
		Integer supplierId = (Integer) request.getSession().getAttribute("MANAGER_SUPPLIER_ID");
		if (supplierId == null) {
			return new ModelAndView("redirect:/manager/login.html");
		}
		int roleId = (int) request.getSession().getAttribute("MANAGER_ROLE_ID");
		System.out.println(roleId);
		ResultDO resultDO = orderService.watiList(supplierId, roleId);

		String waitOrderContent = " <tr class=\"text-c\"> " + "<th style='width:5%'>订单编号</th> "
				+ "<th style='width:10%'>订单类型</th> " + "<th style='width:5%'>服务人数</th> "
				+ "<th style='width:15%'>服务时间</th>  " + "<th style='width:25%'>服务地址</th> "
				+ "<th style='width:10%'>操作</th> " + "</tr>";

		if (resultDO.isSuccess()) {
			List<OrderDO> list = (List<OrderDO>) resultDO.getModel(ResultSupport.FIRST_MODEL_KEY);

			DateFormat dateValueFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");

			for (OrderDO orderDO : list) {
				try {
					String timeStr = "";
					int staffCount = 1;
					System.out.println(orderDO.getwOServiceType());
					if (orderDO.getwOServiceType() == 0 || orderDO.getwOServiceType() >= 11) {

						staffCount = orderDO.getOrderDetailDO().getwOStaffCount();
						timeStr = dateValueFormat.format(orderDO.getOrderDetailDO().getwOSubscribe());
					}
					waitOrderContent += "<tr class=\"text-c\"><td>" + orderDO.getwOOrderNo() + "</td><td>"
							+ orderDO.getwOServiceName() + "</td><td>" + staffCount + "人</td><td>" + timeStr
							+ "</td><td>" + orderDO.getOrderDetailDO().getwOAddress()
							+ "</td><td class=\"td-status\"><button onClick=\"rushOrder(" + orderDO.getId()
							+ ")\">快速抢单</button></td></tr>";
				} catch (Exception e) {
					System.out.println(orderDO.getId());
					// e.printStackTrace();
				}
			}
			mav.addObject("JSON_DATA", waitOrderContent);
		} else {
			waitOrderContent += "<tr class=\"text-c\"><td  colspan='6'><h1>当前没有任何的订单</h1></td></tr>";
			mav.addObject("JSON_DATA", waitOrderContent);
		}
		return mav;
	}

	@RequestMapping(value = "/shortOrderList/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView shortOrderList(ModelAndView mav, @PathVariable(value = "pageNo") Integer pageNo,
			HttpServletRequest request) {

		OrderQuery query = new OrderQuery();
		query.setPage(true);
		query.setPageNO(pageNo);
		query.setPageRows(10);

		ResultDO resultDO = orderService.shortOrderList(query);
		if (resultDO.isSuccess()) {
			List<OrderDO> list = (List<OrderDO>) resultDO.getModel(ResultSupport.FIRST_MODEL_KEY);
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) resultDO.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) resultDO.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);
		} else {
			mav.addObject("JSON_DATA", 0);
		}
		mav.setViewName("manager2/order/shortOrderList");
		return mav;
	}

	@RequestMapping(value = "/rushOrder/{id}.json", method = RequestMethod.GET)
	public ModelAndView rushOrder(ModelAndView mav, @PathVariable(value = "id") Integer id,
			HttpServletRequest request) {
		JsonResultDO jsonResultDO = new JsonResultSupport();
		String adminName = (String) request.getSession().getAttribute("MANAGER_NAME");
		Integer supplierId = (Integer) request.getSession().getAttribute("MANAGER_SUPPLIER_ID");
		String supplierName = (String) request.getSession().getAttribute("MANAGER_SUPPLIER_NAME");
		OrderDO orderDO = new OrderDO();
		orderDO.setId(id);

		orderDO.setwOSupplierId(supplierId);
		orderDO.setModifiAuthor(adminName);
		OrderDetailDO orderDetailDO = new OrderDetailDO();
		orderDetailDO.setId(id);
		orderDetailDO.setwOSupplierName(supplierName);
		orderDetailDO.setModifiAuthor(adminName);
		orderDO.setOrderDetailDO(orderDetailDO);
		orderDO.setStatus(2);// 待分配
		ResultDO result = orderService.rush(orderDO);
		if (result.isSuccess()) {
			jsonResultDO.setInfo("抢单成功");
			jsonResultDO.setStatus(JsonResultDO.JSON_SUCCESS);
			mav.addObject("JSON_DATA", jsonResultDO);
		} else {
			jsonResultDO.setInfo("抢单失败");
			jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
			mav.addObject("JSON_DATA", jsonResultDO);
		}
		return mav;
	}

	@RequestMapping(value = "/assigned/{id}/{serviceType}.json", method = RequestMethod.GET)
	public ModelAndView assigned(ModelAndView mav, @PathVariable(value = "id") Integer id,
			@PathVariable(value = "serviceType") Integer serviceType, HttpServletRequest request) {
		Integer supplierId = (Integer) request.getSession().getAttribute("MANAGER_SUPPLIER_ID");
		if (supplierId == null) {
			return new ModelAndView("redirect:/manager/login.html");
		}

		ResultDO result3 = null;
		JsonResultDO jsonResultDO = new JsonResultSupport();

		StaffQuery squery = new StaffQuery();
		switch (serviceType) {
		case 0:
		case 13:
			squery.setwSType0(1);
			break;
		case 1:
			squery.setwSType1(1);
			break;
		case 2:
			squery.setwSType2(1);
			break;
		case 3:
			squery.setwSType3(1);
			break;
		}
		squery.setSupplierId(supplierId);
		squery.setStatus(1);
		result3 = staffService.list(squery);

		List<String> lists = new ArrayList<String>();

		List<StaffDO> list = (List<StaffDO>) result3.getModel(ResultSupport.FIRST_MODEL_KEY);

		if (list != null) {
			for (StaffDO staffDO : list) {
				Integer ids = staffDO.getId();
				String name = staffDO.getwSName();
				String phone = staffDO.getwSTelephone();
				String str = "编号 " + ids + " 姓名  " + name + " 电话  " + phone;
				System.out.println(str);
				lists.add(str);
			}
		}

		// System.out.println(list+" --------------- "+lists);

		jsonResultDO.setList(lists);
		jsonResultDO.setStatus(JsonResultDO.JSON_SUCCESS);
		mav.addObject("JSON_DATA", jsonResultDO);

		return mav;
	}

	@RequestMapping(value = "/assigned.json", method = RequestMethod.POST)
	public ModelAndView assigned(ModelAndView mav, OrderDO orderDO, HttpServletRequest request) {
		JsonResultDO jsonResultDO = new JsonResultSupport();
		String adminName = (String) request.getSession().getAttribute("MANAGER_NAME");
		orderDO.setStatus(8);
		orderDO.setModifiAuthor(adminName);
		orderDO.setOrderDetailDO(new OrderDetailDO());
		orderDO.getOrderDetailDO().setId(orderDO.getId());
		orderDO.getOrderDetailDO().setModifiAuthor(adminName);
		System.out.println(orderDO.getwOSupplierId());
		if (orderDO.getwOSupplierId() == null) {
			Integer supplierId = (Integer) request.getSession().getAttribute("MANAGER_SUPPLIER_ID");
			if (supplierId == null) {
				return new ModelAndView("redirect:/manager/login.html");
			}
			orderDO.setwOSupplierId(supplierId);
		} else {
			orderDO.getOrderDetailDO().setwOSupplierName(orderDO.getSupplierName());
		}

		ResultDO resultDO = orderService.assigned(orderDO);
		if (resultDO.isSuccess()) {
			jsonResultDO.setInfo("提交成功");
			jsonResultDO.setStatus(JsonResultDO.JSON_SUCCESS);
			mav.addObject("JSON_DATA", jsonResultDO);
		} else {
			jsonResultDO.setInfo("提交失败");
			jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
			mav.addObject("JSON_DATA", jsonResultDO);
		}
		return mav;
	}

	@RequestMapping(value = "/assigned123.json", method = RequestMethod.GET)
	public ModelAndView assignget(ModelAndView mav, HttpServletRequest request) {
		JsonResultDO jsonResultDO = new JsonResultSupport();
		String adminName = (String) request.getSession().getAttribute("MANAGER_NAME");
		String orderid = request.getParameter("id");
		String sfid1 = request.getParameter("sfid1").trim();
		String sfid2 = request.getParameter("sfid2").trim();
		int oId = Integer.parseInt(orderid);

		Integer sfId1 = Integer.valueOf(sfid1);

		List<Integer> sfids = new ArrayList<Integer>();
		sfids.add(sfId1);
		if (!sfid2.equals("undefined")) {
			Integer sfId2 = Integer.valueOf(sfid2);
			sfids.add(sfId2);
		}
		ResultDO result = orderService.get(oId);

		OrderDO orderDO = (OrderDO) result.getModel(ResultSupport.FIRST_MODEL_KEY);
		orderDO.setStaffIds(sfids);
		orderDO.setStatus(3);// 待验收
		orderDO.setModifiAuthor(adminName);
		orderDO.setOrderDetailDO(new OrderDetailDO());
		orderDO.getOrderDetailDO().setId(orderDO.getId());
		orderDO.getOrderDetailDO().setModifiAuthor(adminName);
		System.out.println(orderDO.getwOSupplierId() + "*********");
		if (orderDO.getwOSupplierId() == null) {
			Integer supplierId = (Integer) request.getSession().getAttribute("MANAGER_SUPPLIER_ID");
			if (supplierId == null) {
				return new ModelAndView("redirect:/manager/login.html");
			}
			orderDO.setwOSupplierId(supplierId);
		} else {
			orderDO.getOrderDetailDO().setwOSupplierName(orderDO.getSupplierName());
		}

		ResultDO resultDO = orderService.assigned(orderDO);
		if (resultDO.isSuccess()) {
			jsonResultDO.setInfo("分配成功");
			jsonResultDO.setStatus(JsonResultDO.JSON_SUCCESS);
			mav.addObject("JSON_DATA", jsonResultDO);
		} else {
			jsonResultDO.setInfo("分配失败");
			jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
			mav.addObject("JSON_DATA", jsonResultDO);
		}
		return mav;
	}

	/**
	 * 所有订单，状态不同
	 * 
	 * @param mav
	 * @param status
	 * @param pageNo
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/allOrderList/{status}/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView allOrderList(ModelAndView mav, @PathVariable(value = "status") Integer status,
			@PathVariable(value = "pageNo") Integer pageNo, HttpServletRequest request) {

		Integer supplierId = (Integer) request.getSession().getAttribute("MANAGER_SUPPLIER_ID");
		if (supplierId == null) {
			return new ModelAndView("redirect:/manager/login.html");
		}
		OrderQuery query = new OrderQuery();
		query.setPage(true);
		query.setSupplierId(supplierId);
		query.setPageNO(pageNo);
		query.setPageRows(10);
		query.setStatus(status);

		// 检索时间查询
		String start = request.getParameter("starts");
		String ends = request.getParameter("ends");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (start != null && !("".equals(start))) {
			Date date = null;
			try {
				date = sdf.parse(start + " 00:00:00");
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			query.setStartDate(date);
		}

		if (ends != null && !("".equals(ends))) {
			Date ends2 = null;
			try {
				ends2 = sdf.parse(ends + " 23:59:59");
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			query.setEndDate(ends2);
		}

		ResultDO result = null;
		result = orderService.assignList(query);
		if (result.isSuccess()) {
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);
			mav.setViewName("manager2/order/allOrderList");
		} else {
			mav.setViewName("error");
		}
		// 标记状态
		mav.addObject("sign", status);
		return mav;
	}

	@RequestMapping(value = "/successList/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView successList(ModelAndView mav, @PathVariable(value = "pageNo") Integer pageNo,
			HttpServletRequest request) {

		Integer supplierId = (Integer) request.getSession().getAttribute("MANAGER_SUPPLIER_ID");
		if (supplierId == null) {
			return new ModelAndView("redirect:/manager/login.html");
		}
		OrderQuery query = new OrderQuery();
		query.setPage(true);
		query.setSupplierId(supplierId);
		query.setPageNO(pageNo);
		query.setPageRows(10);

		// 检索时间查询
		String start = request.getParameter("starts");
		String ends = request.getParameter("ends");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (start != null && !("".equals(start))) {
			Date date = null;
			try {
				date = sdf.parse(start + " 00:00:00");
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			query.setStartDate(date);
		}

		if (ends != null && !("".equals(ends))) {
			Date ends2 = null;
			try {
				ends2 = sdf.parse(ends + " 23:59:59");
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			query.setEndDate(ends2);
		}

		ResultDO result = null;
		result = orderService.successList(query);
		if (result.isSuccess()) {
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);
			mav.setViewName("manager2/order/orderSuccessList");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}

	@RequestMapping(value = "/acceptList/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView acceptList(ModelAndView mav, @PathVariable(value = "pageNo") Integer pageNo,
			HttpServletRequest request) {

		Integer supplierId = (Integer) request.getSession().getAttribute("MANAGER_SUPPLIER_ID");
		if (supplierId == null) {
			return new ModelAndView("redirect:/manager/login.html");
		}
		OrderQuery query = new OrderQuery();
		query.setPage(true);
		query.setSupplierId(supplierId);
		query.setPageNO(pageNo);
		query.setPageRows(10);

		ResultDO result = null;
		result = orderService.acceptList(query);
		if (result.isSuccess()) {
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);
			mav.setViewName("manager2/order/acceptList");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}

	@RequestMapping(value = "/failList/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView failList(ModelAndView mav, @PathVariable(value = "pageNo") Integer pageNo,
			HttpServletRequest request) {

		Integer supplierId = (Integer) request.getSession().getAttribute("MANAGER_SUPPLIER_ID");
		if (supplierId == null) {
			return new ModelAndView("redirect:/manager/login.html");
		}
		OrderQuery query = new OrderQuery();
		query.setPage(true);
		query.setSupplierId(supplierId);
		query.setPageNO(pageNo);
		query.setPageRows(10);

		// 检索时间查询
		String start = request.getParameter("starts");
		String ends = request.getParameter("ends");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (start != null && !("".equals(start))) {
			Date date = null;
			try {
				date = sdf.parse(start + " 00:00:00");
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			query.setStartDate(date);
		}

		if (ends != null && !("".equals(ends))) {
			Date ends2 = null;
			try {
				ends2 = sdf.parse(ends + " 23:59:59");
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			query.setEndDate(ends2);
		}

		ResultDO result = null;
		result = orderService.failList(query);
		if (result.isSuccess()) {
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);
			mav.setViewName("manager2/order/failList");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}

	@RequestMapping(value = "/assignedList/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView assignedList(ModelAndView mav, @PathVariable(value = "pageNo") Integer pageNo,
			HttpServletRequest request) {

		Integer supplierId = (Integer) request.getSession().getAttribute("MANAGER_SUPPLIER_ID");
		if (supplierId == null) {
			return new ModelAndView("redirect:/manager/login.html");
		}
		OrderQuery query = new OrderQuery();
		query.setPage(true);
		query.setSupplierId(supplierId);
		query.setPageNO(pageNo);
		query.setPageRows(10);

		// 检索时间查询
		String start = request.getParameter("starts");
		String ends = request.getParameter("ends");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (start != null && !("".equals(start))) {
			Date date = null;
			try {
				date = sdf.parse(start + " 00:00:00");
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			query.setStartDate(date);
		}

		if (ends != null && !("".equals(ends))) {
			Date ends2 = null;
			try {
				ends2 = sdf.parse(ends + " 23:59:59");
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			query.setEndDate(ends2);
		}

		ResultDO result = null;
		result = orderService.assignedList(query);
		if (result.isSuccess()) {
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);
			mav.setViewName("manager2/order/assignedList");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}

	@RequestMapping(value = "/inspectList/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView inspectList(ModelAndView mav, @PathVariable(value = "pageNo") Integer pageNo,
			HttpServletRequest request) {

		Integer supplierId = (Integer) request.getSession().getAttribute("MANAGER_SUPPLIER_ID");
		if (supplierId == null) {
			return new ModelAndView("redirect:/manager/login.html");
		}
		OrderQuery query = new OrderQuery();
		query.setPage(true);
		query.setSupplierId(supplierId);
		query.setPageNO(pageNo);
		query.setPageRows(10);

		// 检索时间查询
		String start = request.getParameter("starts");
		String ends = request.getParameter("ends");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (start != null && !("".equals(start))) {
			Date date = null;
			try {
				date = sdf.parse(start + " 00:00:00");
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			query.setStartDate(date);
		}

		if (ends != null && !("".equals(ends))) {
			Date ends2 = null;
			try {
				ends2 = sdf.parse(ends + " 23:59:59");
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			query.setEndDate(ends2);
		}

		ResultDO result = null;
		result = orderService.inspectList(query);
		if (result.isSuccess()) {
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);
			mav.setViewName("manager2/order/inspectedList");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}

	@RequestMapping(value = "/detail/{id}.html", method = RequestMethod.GET)
	public ModelAndView orderDetail(ModelAndView mav, @PathVariable(value = "id") Integer id,
			HttpServletRequest request) {

		ResultDO result = orderService.get(id);

		if (result.isSuccess()) {
			mav.addObject("ORDER_INFO", result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.setViewName("manager/order/detail");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}

	/**
	 * 订单详情
	 * 
	 * @param mav
	 * @param id
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/detail2/{id}.json", method = RequestMethod.GET)
	public ModelAndView detail2(ModelAndView mav, @PathVariable(value = "id") Integer id, HttpServletRequest request) {
		JsonResultDO jsonResultDO = new JsonResultSupport();
		ResultDO result = orderService.get(id);
		List<OrderDO> list = new ArrayList<OrderDO>();
		if (result.isSuccess()) {
			OrderDO orderDO = (OrderDO) result.getModel(ResultSupport.FIRST_MODEL_KEY);
			list.add(orderDO);
			jsonResultDO.setList(list);
			jsonResultDO.setStatus(JsonResultDO.JSON_SUCCESS);
			mav.addObject("JSON_DATA", jsonResultDO);
		} else {
			jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
			mav.addObject("JSON_DATA", jsonResultDO);
		}
		return mav;
	}

	@RequestMapping(value = "/confirmCanecl/{id}.json", method = RequestMethod.GET)
	public ModelAndView confirmCanecl(ModelAndView mav, @PathVariable(value = "id") Integer id,
			HttpServletRequest request) {
		JsonResultDO jsonResultDO = new JsonResultSupport();
		OrderDO orderDO = new OrderDO();
		orderDO.setId(id);
		orderDO.setStatus(5);
		ResultDO result = orderService.modifi(orderDO);
		if (result.isSuccess()) {
			jsonResultDO.setInfo("取消订单成功");
			jsonResultDO.setStatus(JsonResultDO.JSON_SUCCESS);
			mav.addObject("JSON_DATA", jsonResultDO);
		} else {
			jsonResultDO.setInfo("取消订单失败");
			jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
			mav.addObject("JSON_DATA", jsonResultDO);
		}
		return mav;
	}

	@RequestMapping(value = "/refund/{id}.json", method = RequestMethod.GET)
	public ModelAndView refund(ModelAndView mav, @PathVariable(value = "id") Integer id, HttpServletRequest request) {
		JsonResultDO jsonResultDO = new JsonResultSupport();
		OrderDO orderDO = new OrderDO();
		orderDO.setId(id);
		orderDO.setStatus(8);
		ResultDO result = orderService.modifi(orderDO);
		if (result.isSuccess()) {
			result = orderService.get(id);
			OrderDO o = (OrderDO) result.getModel(ResultSupport.FIRST_MODEL_KEY);
			AccountRecordDO accountRecordDO = new AccountRecordDO();
			accountRecordDO.setwType(2);
			accountRecordDO.setwItem("订单退款");
			BigDecimal money = new BigDecimal(0.00);
			if(o.getwOIsCoupon() == 1) {
				money = o.getwOFee().subtract(o.getwOCoupon());
			} else {
				money = o.getwOFee();
			}
			accountRecordDO.setwMoney(money);
			accountRecordDO.setwDesc("订单号" + o.getwOOrderNo() + "退款成功");
			accountRecordDO.setModifiAuthor("system");
			accountRecordDO.setCreateTime(Calendar.getInstance().getTime());
			accountRecordDO.setCreateAuthor("system");
			accountRecordDO.setStatus(1);
			accountRecordDO.setDatalevel(1);
			supplierAccountService.createRecrod(accountRecordDO);
			jsonResultDO.setInfo("退款订单成功");
			jsonResultDO.setStatus(JsonResultDO.JSON_SUCCESS);
			mav.addObject("JSON_DATA", jsonResultDO);
		} else {
			jsonResultDO.setInfo("退款订单失败");
			jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
			mav.addObject("JSON_DATA", jsonResultDO);
		}
		return mav;
	}

	@RequestMapping(value = "/success/{id}.json", method = RequestMethod.GET)
	public ModelAndView confirmSuccess(ModelAndView mav, @PathVariable(value = "id") Integer id,
			HttpServletRequest request) {
		JsonResultDO jsonResultDO = new JsonResultSupport();
		OrderDO orderDO = new OrderDO();
		orderDO.setId(id);
		orderDO.setStatus(6);
		ResultDO result = orderService.modifi(orderDO);
		if (result.isSuccess()) {
			jsonResultDO.setInfo("订单成功");
			jsonResultDO.setStatus(JsonResultDO.JSON_SUCCESS);
			mav.addObject("JSON_DATA", jsonResultDO);
		} else {
			jsonResultDO.setInfo("修改订单状态失败");
			jsonResultDO.setStatus(JsonResultDO.JSON_FAILED);
			mav.addObject("JSON_DATA", jsonResultDO);
		}
		return mav;
	}

	@RequestMapping(value = "/cancelList/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView list(ModelAndView mav, @PathVariable(value = "pageNo") Integer pageNo,
			HttpServletRequest request) {

		OrderQuery query = new OrderQuery();
		query.setPage(true);
		query.setStatus(4);
		query.setPageNO(pageNo);
		query.setPageRows(10);

		ResultDO result = orderService.list(query);
		if (result.isSuccess()) {
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);
			mav.setViewName("manager/order/cancelList");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}

	@RequestMapping(value = "/list/{orderNo}/{status}/{serviceType}/{supplierId}/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView list(ModelAndView mav, @PathVariable(value = "orderNo") String orderNo,
			@PathVariable(value = "status") Integer status, @PathVariable(value = "serviceType") Integer serviceType,
			@PathVariable(value = "supplierId") Integer supplierId, @PathVariable(value = "pageNo") Integer pageNo,
			HttpServletRequest request) {

		// int supplierId = (int)
		// request.getSession().getAttribute("MANAGER_SUPPLIER_ID");
		OrderQuery query = new OrderQuery();
		query.setPage(true);
		System.out.println(serviceType);
		if (!orderNo.equals("all")) {
			query.setOrderNo(orderNo);
		}
		if (supplierId != 0) {
			query.setSupplierId(supplierId);
		}
		if (status != 0) {
			query.setStatus(status);
		}
		if (serviceType != -1) {
			query.setServiceType(serviceType);
		} else {
			query.setServiceType(serviceType);
		}
		query.setPageNO(pageNo);
		query.setPageRows(10);

		ResultDO result = orderService.list(query);
		System.out.println(result.isSuccess());
		if (result.isSuccess()) {
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);
			mav.addObject("INCOME", result.getModel(ResultSupport.THIRD_MODEL_KEY));
			SupplierQuery squery = new SupplierQuery();
			result = suppliersService.list(squery);
			mav.addObject("SUPPLIER_LIST", result.getModel(ResultSupport.FIRST_MODEL_KEY));
			if (status == 4) {
				mav.setViewName("manager/order/cancel_list");
			} else if (status == 58) {
				mav.setViewName("manager/order/cancel_pay_list");
			} else if (status == 5) {
				mav.setViewName("manager/financial/refund");
			} else {
				mav.setViewName("manager/order/list");
			}
		} else {
			mav.setViewName("error");
		}

		return mav;
	}

	@RequestMapping(value = "/list/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView listWaitAssign(ModelAndView mav, @PathVariable(value = "pageNo") Integer pageNo,
			HttpServletRequest request) {

		Integer supplierId = (Integer) request.getSession().getAttribute("MANAGER_SUPPLIER_ID");
		if (supplierId == null) {
			return new ModelAndView("redirect:/manager/login.html");
		}
		OrderQuery query = new OrderQuery();
		query.setPage(true);
		if (supplierId != 1) {
			query.setSupplierId(supplierId);
		}

		query.setPageNO(pageNo);
		query.setPageRows(18);
		query.setStatus(2);
		query.setServiceType(0);

		ResultDO result = orderService.list(query);
		if (result.isSuccess()) {
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);
		}
		return mav;
	}

	@RequestMapping(value = "/couponlist/{supplierId}/{pageNo}.html", method = RequestMethod.GET)
	public ModelAndView list(ModelAndView mav, @PathVariable(value = "supplierId") Integer supplierId,
			@PathVariable(value = "pageNo") Integer pageNo, HttpServletRequest request) {

		OrderQuery query = new OrderQuery();
		query.setPage(true);
		query.setIsCoupon(1);
		if (supplierId != 0) {
			query.setSupplierId(supplierId);
		}
		query.setPageNO(pageNo);
		query.setPageRows(10);

		ResultDO result = orderService.couponList(query);
		ResultDO result2 = null;
		SupplierQuery query2 = new SupplierQuery();
		result2 = suppliersService.list(query2);
		if (result.isSuccess()) {
			PageBeanUtil pageBean = new PageBeanUtil();
			long count = (Long) result.getModel(ResultSupport.SECOND_MODEL_KEY);
			pageBean.setCurrentPage(pageNo);
			pageBean.setPageSize(query.getPageRows());
			pageBean.setRecordCount(count);
			pageBean.setPageCount(count);
			pageBean.setPages(pageNo);
			pageBean.setDataList((List<Object>) result.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.addObject("PAGE_BEAN", pageBean);
			mav.addObject("FEE_COUNT", result.getModel(ResultSupport.THIRD_MODEL_KEY));
			mav.addObject("COUPON_COUNT", result.getModel(ResultSupport.FORTH_MODEL_KEY));
			mav.addObject("SUPPLIER_LIST", result2.getModel(ResultSupport.FIRST_MODEL_KEY));
			mav.setViewName("manager/order/couponList");
		} else {
			mav.setViewName("error");
		}
		return mav;
	}
}
